//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using OpenLS.Spreadsheet.Expressions;
using OpenLS.Spreadsheet.Parsing;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class FormulaRowsReference : FormulaDimensionsReference<Row, Column>
    {
        public override void ToggleLock()
        {
            Console.WriteLine("todo");
        }
        internal FormulaRowsReference(Token t, FullRangeReference<Row, Column> rangeReference, Formula owner)
            : base(t, rangeReference, owner)
        {
            DimensionReference = new AnchoredRowReference(rangeReference.Range.TopLeftCell.Row, false);
        }
        public FormulaRowsReference(Token t, RowReference cr, Formula owner)
            : base(t, cr, owner)
        {
            RangeReference = new FullRowRangeReference(cr, cr);
        }

        internal override void Extend(CellAddress address, ResizerType type)
        {
            if (DimensionReference != null)
            {
                if (DimensionReference.Anchor.Index == address.Row)
                {
                    if (RangeReference.Range.RowSpan != 1)
                        RangeReference = new FullRowRangeReference(DimensionReference, DimensionReference);
                    return;
                }
                RowReference c2 = (RowReference)DimensionReference.OffsetDim(address - new CellAddress(DimensionReference.Anchor.Index, 0));
                RangeReference = new FullRowRangeReference(DimensionReference, c2);
                // DimensionReference = null;
                return;
            }
            // Contract.Assert(false);
        }

        internal override void Move(NavigationDirection navigationDirection, bool hold)
        {
            if (hold)
            {
                Row anchor = DimensionReference.Anchor;
                Row c1 = ((FullRowRange)RangeReference.Range).FirstRow;
                Row c2 = ((FullRowRange)RangeReference.Range).LastRow;
                Row toMove = c1 == anchor ? c2 : c1;
                Cell topCell = toMove.Worksheet[ toMove.Index, 0];
                topCell = topCell.GetNeighbor(navigationDirection);

                RangeReference = new FullRowRangeReference(new AnchoredRowReference(anchor, false), new AnchoredRowReference(topCell.Row, false));
            }
            else
            {
                DimensionReference = DimensionReference.OffsetDim(GetRelativeAddress(navigationDirection));
                RangeReference = new FullRowRangeReference(DimensionReference, DimensionReference);
            }
        }

        internal void SetDimensionReference(RowReference cr)
        {
            throw new NotImplementedException();
        }
    }
}